package hn.sigit.view;

import hn.sigit.dao.hnd.administrative.HND_PermitRuleGroupDAO;
import hn.sigit.dao.hnd.administrative.HND_SpatialRuleDAO;
import hn.sigit.logic.general.GeneralHelper;
import hn.sigit.logic.general.ResourceBundleHelper;
import hn.sigit.model.hnd.administrative.HND_PermitRuleGroup;
import hn.sigit.model.hnd.administrative.HND_PermitType;
import hn.sigit.model.hnd.administrative.HND_SpatialRule;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("spatialRuleGroupsHelper")
@Scope(value = ScopeType.CONVERSATION)
@AutoCreate
public class SpatialRuleGroupsHelper implements Serializable {
	private static final long serialVersionUID = 1L;

	private List<HND_PermitRuleGroup> spatialRuleGroups;
	private HND_PermitRuleGroup selected;
	private boolean adding;
	
	private List<HND_SpatialRule> selectedSpatialRules;
	
	private static List<SelectItem> permitTypeValueList;
	private List<SelectItem> spatialRuleSelectList;
	private Map<Long,HND_SpatialRule> mapIdToRule;
	private List<String> spatialRulesSelected;
	
	@In
	private ResourceBundleHelper resBundle;
	
	@In
	private GeneralHelper generalHelper;

	
	public List<HND_PermitRuleGroup> getSpatialRuleGroups() {
		if (spatialRuleGroups == null) {
			spatialRuleGroups = HND_PermitRuleGroupDAO.loadPermitRuleGroups();
		}
		return spatialRuleGroups;
	}
	public void setSpatialRuleGroups(List<HND_PermitRuleGroup> spatialRuleGroups) {
		this.spatialRuleGroups = spatialRuleGroups;
	}
	
	public HND_PermitRuleGroup getSelected() {
		return selected;
	}
	public void setSelected(HND_PermitRuleGroup selected) {
		if (this.selected != selected) {
			this.selected = selected;
			selectedSpatialRules = null;
		}
	}
	
	public boolean isAdding() {
		return adding;
	}
	public void setAdding(boolean adding) {
		this.adding = adding;
	}
	
	public List<SelectItem> getPermitTypeValueList() {
		if (permitTypeValueList == null) {
			permitTypeValueList = new ArrayList<SelectItem>();
			permitTypeValueList.add(new SelectItem(null, "(seleccionar)"));
			for (HND_PermitType pt : HND_PermitType.values())
				permitTypeValueList.add(new SelectItem(pt, resBundle.loadMessage(pt.name())));
		}
		return permitTypeValueList;
	}
	
	public List<HND_SpatialRule> getSelectedSpatialRules() {
		if (selectedSpatialRules == null || selectedSpatialRules.size() == 0) {
			if (getSelected() != null) {
				selectedSpatialRules = new ArrayList<HND_SpatialRule>(getSelected().getSpatialRules());
			}
		}
		return selectedSpatialRules;
	}
	
	public List<SelectItem> getSpatialRuleSelectList() {
		if (spatialRuleSelectList == null) {
			spatialRuleSelectList = new ArrayList<SelectItem>();
			Set<HND_SpatialRule> selectedSpatialRuleSet = getSelected().getSpatialRules();
			
			mapIdToRule = new HashMap<Long,HND_SpatialRule>(); 
			
			for (HND_SpatialRule sr : HND_SpatialRuleDAO.loadSpatialRules()) {
				if (selectedSpatialRuleSet == null || !selectedSpatialRuleSet.contains(sr)) {
					mapIdToRule.put(sr.getId(), sr);
					spatialRuleSelectList.add(new SelectItem(
						sr.getId(),
						generalHelper.spatialRuleDetailsText(sr)));
				}
			}
		}
		return spatialRuleSelectList;
	}
	
	public List<String> getSpatialRulesSelected() {
		return spatialRulesSelected;
	}
	public void setSpatialRulesSelected(List<String> spatialRulesSelected) {
		this.spatialRulesSelected = spatialRulesSelected;
	}
	
	public void acceptSpatialRules() {
		if (getSelected() == null || getSpatialRulesSelected() == null) return;
		
		for (String srId : getSpatialRulesSelected())
			getSelected().getSpatialRules().add(mapIdToRule.get(Long.valueOf(srId)));
		
		spatialRuleSelectList = null;
		selectedSpatialRules = null;
	}
	
	public void deleteSpatialRule(HND_SpatialRule sr) {
		if (getSelected() == null || getSelected().getSpatialRules() == null) return;
		
		getSelected().getSpatialRules().remove(sr);
		mapIdToRule.remove(sr.getId());
		
		spatialRuleSelectList = null;
		selectedSpatialRules = null;
	}
	
	public void newPermitRuleGroup() {
		setSelected(new HND_PermitRuleGroup());
		adding = true;
	}
	
	public void editPermitRuleGroup(HND_PermitRuleGroup prg) {
		setSelected(prg);
		adding = false;
	}
	
	public void deletePermitRuleGroup(HND_PermitRuleGroup sr) {
		HND_PermitRuleGroupDAO.delete(sr);
		spatialRuleGroups = null;
	}
	
	public String applyChanges() {
		if (selected != null) {
			if (adding)
				spatialRuleGroups.add(selected);
			
			HND_PermitRuleGroupDAO.save(selected);
		}
			
		return null;
	}
}
